package com.nullpointer.xchatfinal.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.ImageOptions;
import com.nullpointer.xchatfinal.R;
import com.nullpointer.xchatfinal.ViewImageActivity;
import com.nullpointer.xchatfinal.object.SelectFriend;
import com.nullpointer.xchatfinal.utils.Common;
import com.nullpointer.xchatfinal.utils.KiraGameLog;
import com.nullpointer.xchatfinal.utils.Utils;

public class SelectFriendAdapter extends ArrayAdapter<SelectFriend> {

	/**
	 * ViewHolder class for layout.<br />
	 * <br />
	 * Auto-created on 2015-01-29 00:22:58 by Android Layout Finder
	 * (http://www.buzzingandroid.com/tools/android-layout-finder)
	 */
	private static class ViewHolder {
		public final LinearLayout rootView;
		public final ImageView ivAvatar;
		public final TextView tvName;
		public final CheckBox cbSellect;

		private ViewHolder(LinearLayout rootView, ImageView ivAvatar,
				TextView tvName, CheckBox cbSellect) {
			this.rootView = rootView;
			this.ivAvatar = ivAvatar;
			this.tvName = tvName;
			this.cbSellect = cbSellect;
		}

		public static ViewHolder create(LinearLayout rootView) {
			ImageView ivAvatar = (ImageView) rootView
					.findViewById(R.id.iv_avatar);
			TextView tvName = (TextView) rootView.findViewById(R.id.tv_name);
			CheckBox cbSellect = (CheckBox) rootView
					.findViewById(R.id.cb_sellect);
			return new ViewHolder(rootView, ivAvatar, tvName, cbSellect);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder vh;
		if (convertView == null) {
			View view = mInflater.inflate(R.layout.item_lv_select_friend,
					parent, false);
			vh = ViewHolder.create((LinearLayout) view);
			view.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		final SelectFriend item = getItem(position);
		vh.tvName.setText(item.getUserName());
		final String avatarID = item.getAvatarID();
		if (avatarID != null) {
			ImageOptions options = new ImageOptions();
			options.round = 5;
			options.fileCache = true;
			options.memCache = true;
			options.targetWidth = 48;
			options.fallback = R.drawable.ic_no_avatar;
			String url = Utils.getSmallAvatarFacebook(avatarID);
			AQuery aq = new AQuery(convertView);
			aq.id(vh.ivAvatar).image(url, options);
			vh.ivAvatar.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					String avatarLarge = Utils.getLargeAvatarFacebook(avatarID);
					Intent intent = new Intent(context, ViewImageActivity.class);
					intent.putExtra(Common.ExtraKey.IMAGE_URL, avatarLarge);
					context.startActivity(intent);
				}
			});
		} else {
			vh.ivAvatar.setImageResource(R.drawable.ic_no_avatar);
		}
		vh.cbSellect.setChecked(item.isChecked());
		vh.cbSellect.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				item.setChecked(((CheckBox) v).isChecked());
			}
		});
		return vh.rootView;
	}

	private LayoutInflater mInflater;
	private Context context;

	// Constructors
	public SelectFriendAdapter(Context context, List<SelectFriend> objects) {
		super(context, 0, objects);
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
	}

	public SelectFriendAdapter(Context context, SelectFriend[] objects) {
		super(context, 0, objects);
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
	}
}
